import { createRouter, createWebHistory } from 'vue-router';
import { userStore, menuStore } from '@/store';

const LoginPage = () => import('@/views/LoginPage.vue');
const Layout = () => import('@/views/LayoutPage.vue');
const Home = () => import('@/views/Home.vue');

const SystemRole = () => import('@/views/system/role/index.vue');
const SystemUser = () => import('@/views/system/user/index.vue');
const SystemMenu = () => import('@/views/system/menu/index.vue');
const SystemDept = () => import('@/views/system/dept/index.vue');
const SystemPost = () => import('@/views/system/post/index.vue');
const SystemDict = () => import('@/views/system/dict/index.vue');
const SystemConfig = () => import('@/views/system/config/index.vue');
const SystemNotice = () => import('@/views/system/notice/index.vue');
const SystemLog = () => import('@/views/system/log/index.vue');

const MonitorData = () => import('@/views/monitor/data/index.vue');
const MonitorList = () => import('@/views/monitor/list/index.vue');
const MonitorOnline = () => import('@/views/monitor/online/index.vue');
const MonitorRedis = () => import('@/views/monitor/redis/index.vue');
const MonitorServer = () => import('@/views/monitor/server/index.vue');
const MonitorTask = () => import('@/views/monitor/task/index.vue');

const routes = [
	{
		path: '/',
		name: '首页',
		component: Layout,
		children: [
			{
				path: '/',
				name: '首页',
				meta: { parent: { path: '/', name: '首页' } },
				component: Home,
			},
			{
				path: '/system/role',
				name: '系统角色',
				meta: { parent: { path: '/system', name: '系统管理' } },
				component: SystemRole,
			},
			{
				path: '/system/user',
				name: '系统用户',
				meta: { parent: { path: '/system', name: '系统管理' } },
				component: SystemUser,
			},
			{
				path: '/system/menu',
				name: '菜单管理',
				meta: { parent: { path: '/system', name: '系统管理' } },
				component: SystemMenu,
			},
			{
				path: '/system/dept',
				name: '部门管理',
				meta: { parent: { path: '/system', name: '系统管理' } },
				component: SystemDept,
			},
			{
				path: '/system/post',
				name: '岗位管理',
				meta: { parent: { path: '/system', name: '系统管理' } },
				component: SystemPost,
			},
			{
				path: '/system/dict',
				name: '字典管理',
				meta: { parent: { path: '/system', name: '系统管理' } },
				component: SystemDict,
			},
			{
				path: '/system/config',
				name: '参数设置',
				meta: { parent: { path: '/system', name: '系统管理' } },
				component: SystemConfig,
			},
			{
				path: '/system/notice',
				name: '通知公告',
				meta: { parent: { path: '/system', name: '系统管理' } },
				component: SystemNotice,
			},
			{
				path: '/system/log',
				name: '日志管理',
				meta: { parent: { path: '/system', name: '系统管理' } },
				component: SystemLog,
			},

			{
				path: '/monitor/druid',
				name: '数据监控',
				meta: { parent: { path: '/monitor', name: '系统监控' } },
				component: MonitorData,
			},
			{
				path: '/monitor/cacheList',
				name: '缓存列表',
				meta: { parent: { path: '/monitor', name: '系统监控' } },
				component: MonitorList,
			},
			{
				path: '/monitor/online',
				name: '在线用户',
				meta: { parent: { path: '/monitor', name: '系统监控' } },
				component: MonitorOnline,
			},
			{
				path: '/monitor/cache',
				name: '缓存监控',
				meta: { parent: { path: '/monitor', name: '系统监控' } },
				component: MonitorRedis,
			},
			{
				path: '/monitor/server',
				name: '服务监控',
				meta: { parent: { path: '/monitor', name: '系统监控' } },
				component: MonitorServer,
			},
			{
				path: '/monitor/job',
				name: '定时任务',
				meta: { parent: { path: '/monitor', name: '系统监控' } },
				component: MonitorTask,
			},
		],
	},
	{
		path: '/login',
		name: '登陆',
		meta: { parent: { path: '', name: '' } },
		component: LoginPage,
	},
];

const router = createRouter({
	history: createWebHistory(),
	routes,
});

router.beforeEach((to, from) => {
	const store = userStore();
	const menu = menuStore();
	if (store.getTokenInfo === '') {
		router.push('/login');
	}
	// menu.setRoutePath({
	// 	path: to.path,
	// 	name: (to.meta.parent.name ? to.meta.parent.name + '-' : '') + to.name,
	// });
	document.title = to.name;
});

export default router;
